/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-01-30     金鹏       the first version
 */

#include "gps_bd.h"

extern struct CMN_UART uart_device[6];
extern int net_flag;                                            /* 网络状态  0:初始状态 1:发送确认信息  2:接收到确认信息 3:已经断网*/
extern int cm_switch;                                           /* 0:WIFI; 1:4G*/
extern rt_mq_t net_recv_mq;                                     /* 网络消息队列句柄*/
static rt_thread_t gps_handl = RT_NULL;                         /* GPS北斗线程句柄*/

/*
 * BD_Entry
 * @Param NULL
 *
 * Change Logs:
 * Date           Author     Notes
 * 2025-1-30     金鹏                     北斗线程
 */
void BD_Entry(void *parameter)
{
    char ch;
    char buffz[70] = "";
    static char lat[15] = "";
    static char lon[15] = "";

    rt_uint8_t count = 0;
    rt_uint8_t mdelay = 0;

    while(1){
        /* 从串口读取一个字节的数据，没有读取到则等待接收信号量 */
        while (rt_device_read(uart_device[1].uart_serial, -1, &ch, 1) != 1){
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(uart_device[1].uart_rx_sem, RT_WAITING_FOREVER);
        }

        /* frame start/end*/
        if(ch == '$'){
            buffz[count] = '\0';

            if(rt_strstr(buffz, "GNRMC")){
                if(mdelay++ == 9){

                    /* json缓冲区*/
                    char str[100] = "";
                    int count = 0;

                    /* 纬度解析*/
                    char *latitude = rt_strstr(buffz, "A,");
                    latitude = latitude+2;

                    while(*latitude != ','){
                        lat[count++] = *(latitude++);
                    }
                    lat[count] = '\0';

                    count = 0;

                    /* 经度解析*/
                    char *longitude = rt_strstr(buffz,"N,");
                    longitude = longitude+2;
                    while(*longitude != ','){
                        lon[count++] = *(longitude++);
                    }
                    lon[count] = '\0';

                    Postion p;
                    p.lat = atof(lat);
                    p.lon = atof(lon);
                    if(net_flag == 3 && cm_switch == 1){
                        while(rt_mq_send(net_recv_mq, &p, 20) == RT_EFULL){
                            rt_mq_recv(net_recv_mq, &p, 20, RT_WAITING_FOREVER);
                            rt_kprintf("over %d %d\n",(int)p.lat,(int)p.lon);
                        }
                    }
                    else{
                        /* 打包Json发送，xxx°xxx′xxx″需要转换*/
                        //rt_kprintf("xx = %s\n",str);

                        Json_Add(str, "id", '1');
                        Json_Add(str, "lat", lat);
                        Json_Add(str, "lon", lon);
                        Json_Sent("jp-topic", str);

                        while(rt_mq_recv(net_recv_mq, &p, 20, 10) == RT_EOK){
                            char str2[100] = "";
                            sprintf(lat, "%lf",(p.lat));
                            sprintf(lon, "%lf",(p.lon));
                            Json_Add(str2, "id", '1');
                            Json_Add(str2, "plat", lat);
                            Json_Add(str2, "plon", lon);
                            Json_Sent("jp-topic", str2);
                            rt_thread_mdelay(100);
                        }
                    }

                    mdelay = 0;
                }

            }
            count = 0;
            continue ;
        }

        /* frame*/
        if(ch != " "){
            buffz[count++] = ch;
        }

    }
}

/*
 * GPS_BD_Test
 * @Param NULL
 *
 * Change Logs:
 * Date         Author    Notes
 * 2025-1-30     金鹏     GPS北斗测试
 */
ENTRY GPS_BD_Test(void)
{
    /* 北斗初始化*/
    if( CMN_Uart_Init("uart2") != RT_EOK){
        rt_kprintf("GPS_Init failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("GPS_Init Successful\n");
    }

    /* 创建GPS北斗线程*/
    gps_handl = rt_thread_create("gps_bd",
                                   BD_Entry,RT_NULL,
                                   ENTRY_STACK_SIZE,
                                   ENTRY_PRI,
                                   ENTRY_TICK);

    /* LOG*/
    if(gps_handl != RT_NULL){
        rt_kprintf("gps thread create successful!!\n");

        /* 启动北斗线程*/
        rt_thread_startup(gps_handl);
    }else{
        rt_kprintf("gps thread create failed!!\n");

        return -RT_ERROR;
    }

    return RT_EOK;
}
